import request from '@/utils/request'
import type { APIResult } from '@/api/index'

/**
 * 科室管理API接口
 * 提供科室的增删改查等操作
 */

// 科室列表查询参数
interface DepartmentQueryParams {
  hospitalId: number
  name?: string
  level?: string
  status?: string
  currentPage: number
  pageSize: number
}

// 科室详情
interface DepartmentDetail {
  id: number
  name: string
  level: string
  status: string
  description?: string
  hospitalId: number
  hospitalName: string
  parentId?: number
  parentName?: string
  doctorCount: number
  uploader: string
  uploadTime: string
}

// 科室列表响应
interface DepartmentListResponse {
  items: DepartmentDetail[]
  totalCount: number
  currentPage: number
  pageSize: number
}

// 科室表单数据
interface DepartmentFormData {
  id?: number
  hospitalId: number
  name: string
  level: string
  parentId?: number
  status: string
  description?: string
}

/**
 * 获取科室列表
 * @param params 查询参数
 * @returns 科室列表数据
 */
export const getDepartmentList = async (
  params: DepartmentQueryParams
): Promise<APIResult<DepartmentListResponse>> => {
  return request.get('/api/department/list', { params })
}

/**
 * 获取科室详情
 * @param id 科室ID
 * @returns 科室详情数据
 */
export const getDepartmentDetail = async (
  id: number
): Promise<APIResult<DepartmentDetail>> => {
  return request.get(`/api/department/${id}`)
}

/**
 * 新增科室
 * @param data 科室表单数据
 * @returns 操作结果
 */
export const addDepartment = async (
  data: DepartmentFormData
): Promise<APIResult<null>> => {
  return request.post('/api/department', data)
}

/**
 * 更新科室
 * @param data 科室表单数据
 * @returns 操作结果
 */
export const updateDepartment = async (
  data: DepartmentFormData
): Promise<APIResult<null>> => {
  return request.put(`/api/department/${data.id}`, data)
}

/**
 * 删除科室
 * @param id 科室ID
 * @returns 操作结果
 */
export const deleteDepartment = async (
  id: number
): Promise<APIResult<null>> => {
  return request.delete(`/api/department/${id}`)
}

/**
 * 批量删除科室
 * @param ids 科室ID数组
 * @returns 操作结果
 */
export const batchDeleteDepartments = async (
  ids: number[]
): Promise<APIResult<null>> => {
  return request.delete('/api/department/batch', { data: { ids } })
}